package com.nbot.server.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.nbot.server.domain.TransmissstationDO;
import com.nbot.server.service.TransmissstationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nbot.server.domain.EverytwohoursproductionDO;
import com.nbot.server.service.EverytwohoursproductionService;
import com.nbot.server.util.PageUtils;
import com.nbot.server.util.Query;
import com.nbot.server.util.R;
import com.nbot.server.annotation.Log;

/**
 * 
 *
 * @author nbot
 * @email nbot@163.com
 * @date 2018-07-21 16:37:30
 */

@Controller
@RequestMapping("/server/everytwohoursproduction")
public class EverytwohoursproductionController {
    @Autowired
    private EverytwohoursproductionService everytwohoursproductionService;
    @Autowired
    private TransmissstationService transmissstationService;

    @GetMapping()
    @RequiresPermissions("server:everytwohoursproduction:everytwohoursproduction")
    String Everytwohoursproduction() {
        return "server/everytwohoursproduction/everytwohoursproduction" ;
    }

    @ResponseBody
    @Log("查看")
    @GetMapping("/list")
    @RequiresPermissions("server:everytwohoursproduction:everytwohoursproduction")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<EverytwohoursproductionDO> everytwohoursproductionList = everytwohoursproductionService.list(query);
        int total = everytwohoursproductionService.count(query);
        PageUtils pageUtils = new PageUtils(everytwohoursproductionList, total);
        return pageUtils;
    }

    @GetMapping("/chart/{autoid}")
    @RequiresPermissions("server:everytwohoursproduction:everytwohoursproduction")
    String catChart(@PathVariable("autoid") Long autoid, Model model) {
        model.addAttribute("autoid", autoid);
        return "server/everytwohoursproduction/chart";
    }

    @ResponseBody
    @GetMapping("/getList/{autoid}")
    @RequiresPermissions("server:everytwohoursproduction:everytwohoursproduction")
    public R getList(@PathVariable("autoid") Long autoid) {
        //查询列表数据
        EverytwohoursproductionDO everytwohoursproduction = everytwohoursproductionService.get(autoid);
        if(everytwohoursproduction!=null)
        {
            List<String> keys = new ArrayList<>();
            List<Double> values = new ArrayList<>();
            keys.add("1:00");
            values.add(Double.parseDouble(everytwohoursproduction.getOne()));
            keys.add("3:00");
            values.add(Double.parseDouble(everytwohoursproduction.getThree()));
            keys.add("5:00");
            values.add(Double.parseDouble(everytwohoursproduction.getFive()));
            keys.add("7:00");
            values.add(Double.parseDouble(everytwohoursproduction.getSeven()));
            keys.add("9:00");
            values.add(Double.parseDouble(everytwohoursproduction.getNine()));
            keys.add("11:00");
            values.add(Double.parseDouble(everytwohoursproduction.getEleven()));
            keys.add("13:00");
            values.add(Double.parseDouble(everytwohoursproduction.getThirteen()));
            keys.add("15:00");
            values.add(Double.parseDouble(everytwohoursproduction.getFifteen()));
            keys.add("17:00");
            values.add(Double.parseDouble(everytwohoursproduction.getSeventeen()));
            keys.add("19:00");
            values.add(Double.parseDouble(everytwohoursproduction.getNineteen()));
            keys.add("21:00");
            values.add(Double.parseDouble(everytwohoursproduction.getTwentyone()));
            keys.add("23:00");
            values.add(Double.parseDouble(everytwohoursproduction.getTwentythree()));

            return R.ok()
                    .put("kpName", everytwohoursproduction.getStation()+everytwohoursproduction.getDate())
                    .put("keys", keys)
                    .put("values", values);
        }
        return   R.error();
    }

    /**
     * tag
     */
    @GetMapping( "/weiZhi")
    @ResponseBody
    public List<EverytwohoursproductionDO> listWeiZhi(){
        return everytwohoursproductionService.listWeiZhi();
    }

    /**
     * tag
     */
    @GetMapping( "/sumProd")
    @ResponseBody
    @RequiresPermissions("server:everytwohoursproduction:everytwohoursproduction")
    public double SumProduction(@RequestParam Map<String, Object> params)  {
        Query query = new Query(params);
        List<EverytwohoursproductionDO> everytwohoursproductionList = everytwohoursproductionService.list(query);
        if(everytwohoursproductionList!=null) {
            SimpleDateFormat sdfDb = new SimpleDateFormat("yyyy-M-d");
            SimpleDateFormat sdfHtmlHhm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat sdfHtml = new SimpleDateFormat("yyyy-MM-dd");
            Date dtBeginYmd = new Date();
            Date dtEndYmd = new Date();
            Date dtBeginYmdHhm = new Date();
            Date dtEndYmdHhm = new Date();
            Date dtTemp = new Date();
            Calendar cal = Calendar.getInstance();
            double sum = 0;
            try {
                for (Map.Entry<String, Object> entry : query.entrySet()) {
                    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
                    if((entry.getKey().toString().equals("timebegin")
                            ||entry.getKey().toString().equals("timebegin")) && entry.getValue()!= null )
                    {
                        dtBeginYmdHhm = sdfHtmlHhm.parse(entry.getValue().toString());
                        dtBeginYmd = sdfHtml.parse(entry.getValue().toString());
                    }

                    else if((entry.getKey().toString().equals("timeend")
                            ||entry.getKey().toString().equals("timeend") )&& entry.getValue()!= null  )
                    {
                        dtEndYmdHhm = sdfHtmlHhm.parse(entry.getValue().toString());
                        dtEndYmd = sdfHtml.parse(entry.getValue().toString());
                    }
                }

                for(int i=0;i<everytwohoursproductionList.size();i++)
                {
                    EverytwohoursproductionDO everyDo = everytwohoursproductionList.get(i);
                    Date dsDo = sdfDb.parse(everyDo.getDate());
                    System.out.println(sdfDb.parse(everyDo.getDate()));
                    if((dsDo.getTime()>dtBeginYmd.getTime()) &&( dsDo.getTime() <dtEndYmd.getTime()))
                    {
                        sum += Double.parseDouble(everyDo.getOne());
                        sum += Double.parseDouble(everyDo.getThree());
                        sum += Double.parseDouble(everyDo.getFive());
                        sum += Double.parseDouble(everyDo.getSeven());
                        sum += Double.parseDouble(everyDo.getNine());
                        sum += Double.parseDouble(everyDo.getEleven());
                        sum += Double.parseDouble(everyDo.getThirteen());
                        sum += Double.parseDouble(everyDo.getFifteen());
                        sum += Double.parseDouble(everyDo.getSeventeen());
                        sum += Double.parseDouble(everyDo.getNineteen());
                        sum += Double.parseDouble(everyDo.getTwentyone());
                        sum += Double.parseDouble(everyDo.getTwentythree());
                    }
                    if (dsDo.getTime() == dtBeginYmd.getTime())
                    {
                        cal.setTime(dsDo);
                        cal.add(Calendar.HOUR,1);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getOne());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getThree());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getFive());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getSeven());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getNine());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getEleven());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getThirteen());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getFifteen());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getSeventeen());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getNineteen());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getTwentyone());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()>=dtBeginYmdHhm.getTime() && dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getTwentythree());
                        }
                    }
                    if (dsDo.getTime() == dtEndYmd.getTime() && dsDo.getTime() != dtBeginYmd.getTime())
                    {
                        cal.setTime(dsDo);
                        cal.add(Calendar.HOUR,1);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getOne());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getThree());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getFive());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getSeven());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getNine());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getEleven());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getThirteen());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getFifteen());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getSeventeen());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getNineteen());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getTwentyone());
                        }
                        cal.add(Calendar.HOUR,2);// 24小时制
                        dtTemp = cal.getTime();
                        if(dtTemp.getTime()<=dtEndYmdHhm.getTime())
                        {
                            sum += Double.parseDouble(everyDo.getTwentythree());
                        }
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return new BigDecimal(sum).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        return -1;
    }

    /**
     * tag
     */
    @GetMapping( "/setpro")
    @ResponseBody
    @RequiresPermissions("server:everytwohoursproduction:everytwohoursproduction")
    public double SetPro(String setval)  {

        TransmissstationDO transmissstationDO  = transmissstationService.get("1");
        if(transmissstationDO!=null)
        {
            transmissstationDO.setStationname(setval);
            transmissstationService.update(transmissstationDO);
        }else
        {
            transmissstationDO.setAutoid("1");
            transmissstationService.update(transmissstationDO);
        }

        return 0;
    }

    @GetMapping("/add")
    @RequiresPermissions("server:everytwohoursproduction:add")
    String add() {
        return "server/everytwohoursproduction/add" ;
    }

    @GetMapping("/edit/{autoid}")
    @RequiresPermissions("server:everytwohoursproduction:edit")
    String edit(@PathVariable("autoid") Long autoid, Model model) {
            EverytwohoursproductionDO everytwohoursproduction = everytwohoursproductionService.get(autoid);
        model.addAttribute("everytwohoursproduction" , everytwohoursproduction);
        return "server/everytwohoursproduction/edit" ;
    }

    /**
     * 保存
     */
    @Log("保存")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("server:everytwohoursproduction:add")
    public R save(EverytwohoursproductionDO everytwohoursproduction) {
        if (everytwohoursproductionService.save(everytwohoursproduction) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("修改")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("server:everytwohoursproduction:edit")
    public R update(EverytwohoursproductionDO everytwohoursproduction) {
            everytwohoursproductionService.update(everytwohoursproduction);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("server:everytwohoursproduction:remove")
    public R remove( Long autoid) {
        if (everytwohoursproductionService.remove(autoid) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @Log("批量删除")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("server:everytwohoursproduction:batchRemove")
    public R remove(@RequestParam("ids[]") Long[]autoids) {
            everytwohoursproductionService.batchRemove(autoids);
        return R.ok();
    }

}
